package com.hongmeng.operater.service.aliyun.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientException;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.vod.model.v20170321.*;
import com.hongmeng.common.core.domain.AjaxResult;
import com.hongmeng.operater.domain.aliyun.VodVideoEntity;
import com.hongmeng.operater.domain.aliyun.VodVideoUploadAuth;
import com.hongmeng.operater.service.aliyun.IVodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class VodServiceImpl implements IVodService {

    @Autowired
    private IAcsClient iAcsClient;

    @Override
    public AjaxResult getVoidUploadToken(String fileName, String videoTitle, String templateGroupId, Long cateId) {
        CreateUploadVideoRequest request = new CreateUploadVideoRequest();
        request.setTitle(videoTitle);
        request.setFileName(fileName);
        if(cateId!=null) {
            request.setCateId(cateId);
        }
        if(templateGroupId!=null) {
            request.setTemplateGroupId(templateGroupId);
        }
        try {
            CreateUploadVideoResponse response =  iAcsClient.getAcsResponse(request);
            VodVideoUploadAuth auth = new VodVideoUploadAuth();
            auth.setUploadAddress(response.getUploadAddress());
            auth.setUploadAuth(response.getUploadAuth());
            auth.setVideoId(response.getVideoId());
            return AjaxResult.success(auth);
        } catch (ClientException e) {
            log.error(e.getMessage(),e);
            return AjaxResult.error(e.getMessage());
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getErrMsg());
        }
    }

    @Override
    public AjaxResult refreshUploadVideo(String videoId) {
        RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
        request.setVideoId(videoId);

        try {
            RefreshUploadVideoResponse response = iAcsClient.getAcsResponse(request);
            VodVideoUploadAuth auth = new VodVideoUploadAuth();
            auth.setUploadAddress(response.getUploadAddress());
            auth.setUploadAuth(response.getUploadAuth());
            auth.setVideoId(response.getVideoId());
            return AjaxResult.success(auth);
        } catch (ClientException e) {
            log.error(e.getMessage(),e);
            return AjaxResult.error(e.getMessage());
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public AjaxResult getPlayUserByVideoId(String videoId) {
        VodVideoEntity vodVideoDto = new VodVideoEntity();
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        try {
            GetPlayInfoResponse response =  iAcsClient.getAcsResponse(request);
            vodVideoDto.setPlayUrlLD(response.getPlayInfoList().get(0).getPlayURL());
            if (response.getPlayInfoList().size()==2){
                vodVideoDto.setPlayUrlSD(response.getPlayInfoList().get(1).getPlayURL());
            }
            vodVideoDto.setVideoTitle(response.getVideoBase().getTitle());
            vodVideoDto.setCoverURL(response.getVideoBase().getCoverURL());
            vodVideoDto.setVideoId(videoId);
            vodVideoDto.setDuration(response.getVideoBase().getDuration());
            log.info(JSONObject.toJSONString(response));
            return AjaxResult.success(vodVideoDto);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    @Override
    public AjaxResult getVodOriginalFileById(String videoId) {
        VodVideoEntity vodVideoDto = new VodVideoEntity();
        GetMezzanineInfoRequest request = new GetMezzanineInfoRequest();
        request.setVideoId(videoId);
        //源片下载地址过期时间
        request.setAuthTimeout(3600L);

        GetMezzanineInfoResponse response;
        try {
            response = iAcsClient.getAcsResponse(request);
        } catch (com.aliyuncs.exceptions.ClientException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        assert response != null;
        vodVideoDto.setDownloadFileUrl( response.getMezzanine().getFileURL());
        vodVideoDto.setDownloadFileName(response.getMezzanine().getFileName());
        vodVideoDto.setVideoId(response.getMezzanine().getVideoId());
        vodVideoDto.setVideoTitle(response.getMezzanine().getFileName());
        vodVideoDto.setDuration(String.valueOf(response.getMezzanine().getDuration()));
        vodVideoDto.setSize(response.getMezzanine().getSize());
        return AjaxResult.success(vodVideoDto);
    }
}
